# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_declaration_information_impl.py
# @Desc    : 申报信息
from app import db
from infrastructure.po.mode.jasco_logistics_trajectory import (
    JascoLogisticsTrajectory,
)
from sqlalchemy import and_, or_, desc, asc


class JascoLogisticsTrajectoryImpl(object):

    @classmethod
    def get_logistics_trajectory_list(cls, order_id, page=1, page_size=999):
        query = db.session.query(JascoLogisticsTrajectory).filter(
            JascoLogisticsTrajectory.order_id == order_id
        )
        return (
            query.order_by(JascoLogisticsTrajectory.create_time.desc()).paginate(
                page=page, per_page=page_size
            ),
            query.count(),
        )

    @classmethod
    def add_logistics_trajectory(
        cls,
        order_id,
            status,
            date,
            content,
            location
    ):

        jasco_logistics_trajectory = JascoLogisticsTrajectory(
            order_id=order_id,
            status=status,
            date=date,
            content=content,
            location=location
        )
        db.session.add(jasco_logistics_trajectory)
        db.session.flush()
        db.session.commit()
        return True

    @classmethod
    def edit_logistics_trajectory(
        cls,
        id,
        order_id,
            status,
            date,
            content,
            location
    ):
        jasco_logistics_trajectory = (
            db.session.query(JascoLogisticsTrajectory).filter(and_(JascoLogisticsTrajectory.id == id, JascoLogisticsTrajectory.order_id == order_id)).first()
        )
        if not jasco_logistics_trajectory:
            return False
        jasco_logistics_trajectory.status = status
        jasco_logistics_trajectory.date = date
        jasco_logistics_trajectory.content = content
        jasco_logistics_trajectory.location = location
        db.session.commit()
        return True

    @classmethod
    def delete_logistics_trajectory(cls, id, order_id):
        jasco_logistics_trajectory = (
            db.session.query(JascoLogisticsTrajectory).filter(and_(JascoLogisticsTrajectory.id == id, JascoLogisticsTrajectory.order_id == order_id)).first()
        )
        if not jasco_logistics_trajectory:
            return False
        db.session.delete(jasco_logistics_trajectory)
        db.session.commit()
        return True
